package com.foreverwin.jzymes.common.controller;

import com.foreverwin.jzymes.common.constant.Constants;
import com.foreverwin.jzymes.common.domain.FieldGroup;
import com.foreverwin.jzymes.common.mapper.CustomFieldsAdviceMapper;
import com.foreverwin.jzymes.common.mapper.CustomerMapper;
import com.foreverwin.jzymes.common.mapper.ItemAdviceMapper;
import com.foreverwin.jzymes.common.mapper.StorageLocationAdviceMapper;
import com.foreverwin.jzymes.common.service.*;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.common.util.CollectionUtil;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.base.AjaxResult;
import com.foreverwin.jzymes.framework.exception.BusinessException;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.framework.util.ServletUtils;
import com.foreverwin.jzymes.production.cutter.mapper.ToolBreakAdiviceMapper;
import com.foreverwin.jzymes.production.picking.domain.PickingBillLog;
import com.foreverwin.jzymes.production.picking.mapper.PickingBillLogMapper;
import com.foreverwin.jzymes.production.picking.mapper.PickingMapper;
import com.foreverwin.jzymes.production.sfc.domain.SFC;
import com.foreverwin.jzymes.production.sfc.enums.ProcedureEnum;
import com.foreverwin.jzymes.production.sfc.mapper.SFCAdviceMapper;
import com.foreverwin.jzymes.production.sfc.mapper.SFCMapper;
import com.foreverwin.jzymes.production.tool.domain.TOOL;
import com.foreverwin.jzymes.production.tool.domain.ToolBill;
import com.foreverwin.jzymes.production.tool.service.IToolBillService;
import com.foreverwin.jzymes.qc.point.mapper.FindUndoneMapper;
import com.foreverwin.jzymes.report.cpk.mapper.CPKReportMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/fieldSelect")
public class FieldController {
    @Autowired
    ICommonService commonService;
    @Autowired
    CustomerMapper customerMapper;
    @Autowired
    ICustomerService customerService;
    @Autowired
    IFieldGroupAdviceService fieldGroupAdviceService;
    @Autowired
    IItemAdviceService itemAdviceService;
    @Autowired
    IItemGroupAdviceService itemGroupAdviceService;
    @Autowired
    ICustomFieldsAdviceService customFieldsAdviceService;
    @Autowired
    IResourceTypeAdviceService resourceTypeAdviceService;
    @Autowired
    INCGroupAdviceService ncGroupAdviceService;
    @Autowired
    IResourceAdviceService resourceAdviceService;
    @Autowired
    INCCodeAdviceService ncCodeAdviceService;
    @Autowired
    IContainerAdviceService containerAdviceService;
    @Autowired
    SFCMapper sfcMapper;
    @Autowired
    ISystemRuleAdviceService systemRuleAdviceService;
    @Autowired
    SFCAdviceMapper sfcAdviceMapper;
    @Autowired
    CustomFieldsAdviceMapper customFieldsAdviceMapper;
    @Autowired
    StorageLocationAdviceMapper storageLocationAdviceMapper;
    @Autowired
    PickingMapper pickingMapper;
    @Autowired
    PickingBillLogMapper pickingBillLogMapper;
    @Autowired
    ItemAdviceMapper itemAdviceMapper;
    @Autowired
    ToolBreakAdiviceMapper toolBreakAdiviceMapper;
    @Autowired
    IToolAdiviceService toolAdiviceService;
    @Autowired
    IToolBillService ToolBillService;
    @Autowired
    FindUndoneMapper findUndoneMapper;
    @Autowired
    CPKReportMapper cpkReportMapper;
    @Autowired
    IDCGroupAdviceService dcGroupAdviceService;

    /**
     * 制程列表
     *
     * @return
     */
    @RequestMapping("/procedure")
    public AjaxResult findProcedureList(  ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        List<FieldGroup> fieldList = fieldGroupAdviceService.findFieldListByGroup( site, Constants.FIELD_GROUP_PROCEDURE );
        return AjaxResult.returnSuccess().put( "value", fieldList );
    }

    /**
     * 机种物料列表
     *
     * @return
     */
    @RequestMapping("/machineType/item")
    public AjaxResult findItemList(  ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        String itemGroup = itemGroupAdviceService.findItemGroupBySite( site );
        List<Map<String,Object>> itemMemberList =  itemGroupAdviceService.findItemGroupMember( site, itemGroup );

        List<Map<String,Object>> rtnItemList = new ArrayList<>();
        for( Map<String,Object> itemMemberMap : itemMemberList ){
            Map<String,Object> itemMap = itemAdviceService.existItem( (String) itemMemberMap.get( "ITEM_BO" ) );

            Map<String,Object> rtnItemMap = new HashMap<>();
            rtnItemMap.put( "machineType", itemGroup );
            rtnItemMap.put( "itemBo", itemMap.get( "HANDLE" ) );
            rtnItemMap.put( "item", itemMap.get( "ITEM" ) );
            rtnItemMap.put( "revision", itemMap.get( "REVISION" ) );
            rtnItemMap.put( "description", customFieldsAdviceService.existCustomValue((String) itemMap.get( "HANDLE" ), Constants.ITEM_CUSTOM_FIELD_ITEM_LABEL ) );
            rtnItemMap.put( "currentVersion", itemMap.get( "CURRENT_REVISION" ) );
            rtnItemMap.put( "unit", itemMap.get( "UNIT_OF_MEASURE" ) );
            rtnItemMap.put( "routerBo", itemMap.get( "ROUTER_BO" ) );
            rtnItemMap.put( "router", BOHandleUtil.splitRouterHandle((String) itemMap.get( "ROUTER_BO" ))[1]  );
            rtnItemMap.put( "routerVersion", BOHandleUtil.splitRouterHandle((String) itemMap.get( "ROUTER_BO" ))[3] );
            rtnItemMap.put( "planQty", customFieldsAdviceMapper.selectCustomValue((String) itemMap.get( "HANDLE" ), Constants.ITEM_CUSTOM_FIELD_PLAN_QTY ) );
            rtnItemList.add( rtnItemMap );
        }
        return AjaxResult.returnSuccess().put( "value", rtnItemList );
    }

    @RequestMapping("/itemGroup/item")
    public AjaxResult findItemByItemGroup( @RequestParam String itemGroup ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        Assert.hasText( itemGroup, I18nUtil.getI18nText( "common.itemGroupNotBlank" ));

        /**机种*/
        String _itemGroup = itemGroupAdviceService.findItemGroupBySite( site );

        List<Map<String,Object>> itemMemberList =  itemGroupAdviceService.findItemGroupMember( site, itemGroup );

        List<Map<String,Object>> rtnItemList = new ArrayList<>();
        for( Map<String,Object> itemMemberMap : itemMemberList ){
            Map<String,Object> itemMap = itemAdviceService.existItem( (String) itemMemberMap.get( "ITEM_BO" ) );

            String itemLabel = customFieldsAdviceService.existCustomValue((String) itemMap.get( "HANDLE" ), Constants.ITEM_CUSTOM_FIELD_ITEM_LABEL );
            String description = StringUtils.isBlank( itemLabel ) ? (String) itemMap.get( "DESCRIPTION" ) : itemLabel;

            Map<String,Object> rtnItemMap = new HashMap<>();
            rtnItemMap.put( "machineType", _itemGroup );
            rtnItemMap.put( "itemBo", itemMap.get( "HANDLE" ) );
            rtnItemMap.put( "item", itemMap.get( "ITEM" ) );
            rtnItemMap.put( "revision", itemMap.get( "REVISION" ) );
            rtnItemMap.put( "description", description );
            rtnItemMap.put( "currentVersion", itemMap.get( "CURRENT_REVISION" ) );
            rtnItemMap.put( "unit", itemMap.get( "UNIT_OF_MEASURE" ) );
            rtnItemMap.put( "storageLocation", itemMap.get( "ERP_PUTAWAY_STORLOC" ) );
            rtnItemMap.put( "itemType", itemMap.get( "ITEM_TYPE" ) );
            rtnItemList.add( rtnItemMap );
        }
        CollectionUtil.sort( rtnItemList, "description" );

        return AjaxResult.returnSuccess().put( "value", rtnItemList );
    }

    /**
     * 验证物料组物料
     *
     * @param itemGroup
     * @param item
     * @return
     */
    @RequestMapping("/itemGroup/existItem")
    public AjaxResult existItemByItemGroup( @RequestParam String itemGroup, @RequestParam String item ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        Assert.hasText( itemGroup, I18nUtil.getI18nText( "common.itemGroupNotBlank" ));
        Assert.hasText( item, I18nUtil.getI18nText( "common.itemNotBlank" ) );

        Map<String,Object> itemMap = itemAdviceService.existItem( site, item );

        List<Map<String,Object>> itemMemberList =  itemGroupAdviceService.findItemGroupMember( site, itemGroup );

        Map<String,Object> rtnItemMap = new HashMap<>();
        for( Map<String,Object> itemMemberMap : itemMemberList ){
            Map<String,Object> _itemMap = itemAdviceService.existItem( (String) itemMemberMap.get( "ITEM_BO" ) );
            if( itemMap.get( "HANDLE" ).equals( itemMemberMap.get( "ITEM_BO" ) ) ){

                String itemLabel = customFieldsAdviceService.existCustomValue((String) itemMap.get( "HANDLE" ), Constants.ITEM_CUSTOM_FIELD_ITEM_LABEL );
                String description = StringUtils.isBlank( itemLabel ) ? (String) itemMap.get( "DESCRIPTION" ) : itemLabel;

                rtnItemMap.put( "itemBo", itemMap.get( "HANDLE" ) );
                rtnItemMap.put( "item", itemMap.get( "ITEM" ) );
                rtnItemMap.put( "revision", itemMap.get( "REVISION" ) );
                rtnItemMap.put( "description", description );
                rtnItemMap.put( "currentVersion", itemMap.get( "CURRENT_REVISION" ) );
                rtnItemMap.put( "unit", itemMap.get( "UNIT_OF_MEASURE" ) );
                rtnItemMap.put( "storageLocation", itemMap.get( "ERP_PUTAWAY_STORLOC" ) );
                rtnItemMap.put( "itemType", itemMap.get( "ITEM_TYPE" ) );
                break;
            }
        }

        if( rtnItemMap.isEmpty() ){
            throw BusinessException.build( "common.itemNotMatchItemGroup", new String[]{ item, itemGroup } );
        }else{
            return AjaxResult.returnSuccess().put( "value", rtnItemMap );
        }

    }

    /**
     * 验证物料清单物料
     *
     * @param item
     * @return
     */
    @RequestMapping("/Bom/existItem")
    public AjaxResult existItemByBom( @RequestParam String item ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        Map<String,Object> itemMap = itemAdviceService.existItem( site, item );
        String machineType = itemGroupAdviceService.findItemGroupBySite( site );
        List< Map<String,Object> >  bomcomponentlList = itemAdviceService.findBomcomponentlList( site , machineType );
        if ( bomcomponentlList == null ){
            throw BusinessException.build("production.picking.SiteBomnotExist",new String[]{site,machineType});
        }
        Map<String,Object> rtnItemMap = new HashMap<>();
        for( Map<String,Object> itemMemberMap : bomcomponentlList ){

            if( itemMap.get( "HANDLE" ).equals( itemMemberMap.get( "ITEM_BO" ) ) ){
                rtnItemMap.put( "itemBo", itemMap.get( "HANDLE" ) );
                rtnItemMap.put( "item", itemMap.get( "ITEM" ) );
                rtnItemMap.put( "description", itemMap.get("DESCRIPTION") );
                rtnItemMap.put( "unit", itemMap.get( "UNIT_OF_MEASURE" ) );

                break;
            }
        }

        if( rtnItemMap.isEmpty() ){
            throw BusinessException.build( "common.itemNotMatchItemGroup", new String[]{ item, machineType } );
        }else{
            return AjaxResult.returnSuccess().put( "value", "" );
        }

    }



    /**
     * 物料
     *
     * @param item
     * @return
     */
    @RequestMapping("/item")
    public AjaxResult findItem( @RequestParam String item ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        Assert.hasText( item, I18nUtil.getI18nText( "common.itemNotBlank" ) );
        Map<String,Object> itemMap = itemAdviceService.existItem( site, item );

        String itemLabel = customFieldsAdviceService.existCustomValue((String) itemMap.get( "HANDLE" ), Constants.ITEM_CUSTOM_FIELD_ITEM_LABEL );
        String description = StringUtils.isBlank( itemLabel ) ? (String) itemMap.get( "DESCRIPTION" ) : itemLabel;
        String itemType = (String) itemMap.get( "ITEM_TYPE" );
        String routerBo = null;
        String router = null;
        String routerVersion = null;
        if( "M".equals( itemType ) ){
            routerBo = (String) itemMap.get( "ROUTER_BO" );
            router = BOHandleUtil.splitRouterHandle( routerBo )[1];
            routerVersion = BOHandleUtil.splitRouterHandle( routerBo )[3];
        }

        Map<String,Object> rtnItemMap = new HashMap<>();
        rtnItemMap.put( "itemBo", itemMap.get( "HANDLE" ) );
        rtnItemMap.put( "item", itemMap.get( "ITEM" ) );
        rtnItemMap.put( "revision", itemMap.get( "REVISION" ) );
        rtnItemMap.put( "description", description );
        rtnItemMap.put( "currentVersion", itemMap.get( "CURRENT_REVISION" ) );
        rtnItemMap.put( "unit", itemMap.get( "UNIT_OF_MEASURE" ) );
        rtnItemMap.put( "routerBo", routerBo );
        rtnItemMap.put( "router", router  );
        rtnItemMap.put( "routerVersion", routerVersion );
        rtnItemMap.put( "planQty", customFieldsAdviceMapper.selectCustomValue((String) itemMap.get( "HANDLE" ), Constants.ITEM_CUSTOM_FIELD_PLAN_QTY ) );

        return AjaxResult.returnSuccess().put( "value", rtnItemMap );
    }

    /**
     * 工令列表
     *
     * @return
     */
    @RequestMapping("/machineType/sfc")
    public AjaxResult findSfcList( ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));

        /**查询机种*/
        String machineType = itemGroupAdviceService.findItemGroupBySite( site );
        List<SFC> sfcList = sfcAdviceMapper.selectSFCListOnWorkByMachineType( site, machineType );
        List<Map<String,Object>> rtnSfcList = new ArrayList<>();
        for( SFC sfc : sfcList ){
            Map<String,Object> sfcMap = new HashMap<>();
            Map<String,Object> itemMap = itemAdviceService.existItem( sfc.getItemBo() );

            sfcMap.put( "sfc", sfc.getSfc() );
            sfcMap.put( "machineType", sfc.getMachineType() );
            sfcMap.put( "procedureBo", sfc.getProcedureBo() );
            sfcMap.put( "procedure", BOHandleUtil.splitFieldGroupHandle( sfc.getProcedureBo() )[1] );
            sfcMap.put( "itemBo", sfc.getItemBo() );
            sfcMap.put( "item", itemMap.get("ITEM") );
            sfcMap.put( "description", customFieldsAdviceService.existCustomValue((String) itemMap.get( "HANDLE" ), Constants.ITEM_CUSTOM_FIELD_ITEM_LABEL ) );
            sfcMap.put( "sfcQty", sfc.getSfcQty());
            sfcMap.put( "routerBo", sfc.getRouterBo() );
            sfcMap.put( "planStartTime", sfc.getPlanStartTime() );
            rtnSfcList.add( sfcMap );
        }
        return AjaxResult.returnSuccess().put( "value", rtnSfcList );
    }

    /**
     * 工令列表
     *
     * @return
     */
    @RequestMapping("/item/sfc")
    public AjaxResult findSfcList( @RequestParam String itemBo ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));

        List<SFC> sfcList = null;
        if( StringUtils.isNotBlank( itemBo ) ){
            String procedureBo = BOHandleUtil.buildFieldGroupHandle( site, ProcedureEnum.CNC.name() );
            sfcList = sfcAdviceMapper.selectSFCListOnWork( site, procedureBo, itemBo );

        }else{
            /**查询机种*/
            String machineType = itemGroupAdviceService.findItemGroupBySite( site );
            sfcList = sfcAdviceMapper.selectSFCListOnWorkByMachineType( site, machineType );
        }

        List<Map<String,Object>> rtnSfcList = new ArrayList<>();
        for( SFC sfc : sfcList ){
            Map<String,Object> sfcMap = new HashMap<>();
            Map<String,Object> itemMap = itemAdviceService.existItem( sfc.getItemBo() );

            sfcMap.put( "sfc", sfc.getSfc() );
            sfcMap.put( "machineType", sfc.getMachineType() );
            sfcMap.put( "procedureBo", sfc.getProcedureBo() );
            sfcMap.put( "procedure", BOHandleUtil.splitFieldGroupHandle( sfc.getProcedureBo() )[1] );
            sfcMap.put( "itemBo", sfc.getItemBo() );
            sfcMap.put( "item", itemMap.get( "ITEM" ) );
            sfcMap.put( "revision", itemMap.get( "REVISION" ) );
            sfcMap.put( "description", customFieldsAdviceService.existCustomValue((String) itemMap.get( "HANDLE" ), Constants.ITEM_CUSTOM_FIELD_ITEM_LABEL ) );
            sfcMap.put( "unit", itemMap.get( "UNIT_OF_MEASURE" ) );
            sfcMap.put( "routerBo", sfc.getRouterBo() );
            sfcMap.put( "planStartTime", sfc.getPlanStartTime() );
            rtnSfcList.add( sfcMap );
        }
        return AjaxResult.returnSuccess().put( "value", rtnSfcList );
    }

    @RequestMapping("/sfc")
    public AjaxResult findSfc( @RequestParam String sfc ){
        String site = CommonMethods.getSite();
        Assert.hasText( sfc, I18nUtil.getI18nText( "common.sfcNotBlank" ) );
        String itemBo = ServletUtils.getParameter( "itemBo" );

        SFC _sfc = sfcMapper.selectSFCById( BOHandleUtil.buildSFCHandle( site, sfc ) );
        Assert.notNull( _sfc, I18nUtil.getI18nText( "production.sfc.sfcNotExist", new String[]{ sfc } ) );
        if( _sfc.getActEndTime() != null ){
            throw BusinessException.build( "production.sfc.sfcDone", new String[]{ sfc } );
        }

        if( StringUtils.isNotBlank( itemBo ) && !_sfc.getItemBo().equals( itemBo ) ){
            throw BusinessException.build( "production.sfc.sfcNotMatchItem", new String[]{ sfc, itemBo } );
        }

        Map<String,Object> itemMap = itemAdviceService.existItem( _sfc.getItemBo() );
        Map<String,Object> sfcMap = new HashMap<>();
        sfcMap.put( "sfc", _sfc.getSfc() );
        sfcMap.put("sfcQty",_sfc.getSfcQty());
        sfcMap.put( "machineType", _sfc.getMachineType() );
        sfcMap.put( "procedureBo", _sfc.getProcedureBo() );
        sfcMap.put( "procedure", BOHandleUtil.splitFieldGroupHandle( _sfc.getProcedureBo() )[1] );
        sfcMap.put( "itemBo", _sfc.getItemBo() );
        sfcMap.put( "item", itemMap.get( "ITEM" ) );
        sfcMap.put( "revision", itemMap.get( "REVISION" ) );
        sfcMap.put( "description", customFieldsAdviceService.existCustomValue((String) itemMap.get( "HANDLE" ), Constants.ITEM_CUSTOM_FIELD_ITEM_LABEL ) );
        sfcMap.put( "unit", itemMap.get( "UNIT_OF_MEASURE" ) );
        sfcMap.put( "routerBo", _sfc.getRouterBo() );
        sfcMap.put( "planStartTime", _sfc.getPlanStartTime() );

        return AjaxResult.returnSuccess().put( "value", sfcMap );
    }

    /**
     * 资源类型资源列表
     *
     * @param resourceType
     * @return
     */
    @RequestMapping("/resourceType/resource")
    public AjaxResult findResourceList( @RequestParam String resourceType ){
        String site = CommonMethods.getSite();
        Assert.hasText( resourceType, I18nUtil.getI18nText( "common.resourceTypeNotBlank" ) );

        String[] resourceTypeArray = resourceType.split( ";" );
        List<Map<String,String>> rtnResourceList = new ArrayList<>();
        for( String rType : resourceTypeArray ){
            List<Map<String,String>> resourceList = resourceTypeAdviceService.findResourceList( site, rType );
            for( Map<String,String> resourceMap : resourceList ){
                Map<String,String> rtnResourceMap = new HashMap<>();
                rtnResourceMap.put( "resrce", resourceMap.get( "RESRCE" ) );
                rtnResourceMap.put( "description", resourceMap.get( "DESCRIPTION" ) );
                rtnResourceList.add( rtnResourceMap );
            }
        }

        return AjaxResult.returnSuccess().put( "value", rtnResourceList );
    }

    /**
     * 验证资源
     *
     * @param resrce
     * @return
     */
    @RequestMapping("/resourceType/existResource")
    public AjaxResult existResourceByResourceType( @RequestParam String resrce, @RequestParam String resourceType ){
        String site = CommonMethods.getSite();
        Assert.hasText( resrce, I18nUtil.getI18nText( "common.resourceNotBlank" ) );
        Assert.hasText( resourceType, I18nUtil.getI18nText( "common.resourceTypeNotBlank" ) );

        Map<String,Object> resourceMap = resourceAdviceService.existResource( site, resrce );

        String[] resourceTypeArray = resourceType.split( ";" );
        Map<String,Object> rtnResourceMap = new HashMap<>();
        for( String rType : resourceTypeArray ){
            List<Map<String,String>> resourceList = resourceTypeAdviceService.findResourceList( site, rType );
            if( resourceList.contains( resourceMap ) ){
                rtnResourceMap.put( "resrce", resourceMap.get( "RESRCE" ) );
                rtnResourceMap.put( "description", resourceMap.get( "DESCRIPTION" ) );
            }
        }

        if( rtnResourceMap.isEmpty() ){
            throw BusinessException.build( "common.resourceNotMatchResourceType", new String[]{ resrce, resourceType } );
        }else{
            return AjaxResult.returnSuccess().put( "value", rtnResourceMap );
        }
    }

    /**
     * 资源类型资源列表
     *
     * @param resourceType
     * @return
     */
    @RequestMapping("/cncType/resource")
    public AjaxResult findResourceListByCncType( @RequestParam String cncType, @RequestParam String resourceType ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ) );

        List<Map<String,Object>> resourceList = resourceAdviceService.findResourceListByCncType( site, cncType, resourceType );
        List<Map<String,String>> rtnResourceList = new ArrayList<>();
        for( Map<String,Object> resourceMap : resourceList ){
            Map<String,String> rtnResourceMap = new HashMap<>();
            rtnResourceMap.put( "resrce", (String) resourceMap.get( "RESRCE" ));
            rtnResourceMap.put( "description", (String) resourceMap.get( "DESCRIPTION" ));
            rtnResourceList.add( rtnResourceMap );
        }

        return AjaxResult.returnSuccess().put( "value", rtnResourceList );
    }

    /**
     * 验证夹位资源类型资源
     *
     * @param resrce
     * @param cncType
     * @param resourceType
     * @return
     */
    @RequestMapping("/cncType/existResource")
    public AjaxResult existResourceByCncType( @RequestParam String resrce, @RequestParam String cncType, @RequestParam String resourceType ){
        String site = CommonMethods.getSite();
        Assert.hasText( resrce, I18nUtil.getI18nText( "common.resourceNotBlank" ) );

        Map<String,Object> resourceMap = resourceAdviceService.existResource( site, resrce );

        Map<String,Object> rtnResourceMap = new HashMap<>();
        List<Map<String,Object>> resourceList = resourceAdviceService.findResourceListByCncType( site, cncType, resourceType );
        if( resourceList.contains( resourceMap ) ){
            rtnResourceMap.put( "resrce", resourceMap.get( "RESRCE" ) );
            rtnResourceMap.put( "description", resourceMap.get( "DESCRIPTION" ) );
        }

        if( rtnResourceMap.isEmpty() ){
            if( StringUtils.isNotBlank( cncType ) ){
                throw BusinessException.build( "common.resourceNotMatchResourceType", new String[]{ resrce, cncType } );
            }else{
                throw BusinessException.build( "common.resourceNotMatchResourceType", new String[]{ resrce, resourceType } );
            }
        }else{
            return AjaxResult.returnSuccess().put( "value", rtnResourceMap );
        }
    }

    /**
     * 资源
     *
     * @param resrce
     * @return
     */
    @RequestMapping("/resource")
    public AjaxResult findResource( @RequestParam String resrce ){
        String site = CommonMethods.getSite();
        Assert.hasText( resrce, I18nUtil.getI18nText( "common.resourceNotBlank" ) );

        Map<String,Object> resourceMap = resourceAdviceService.existResource( site, resrce );
        Map<String,Object> rtnResourceMap = new HashMap<>();
        rtnResourceMap.put( "resrce", resourceMap.get( "RESRCE" ) );
        rtnResourceMap.put( "description", resourceMap.get( "DESCRIPTION" ) );
        return AjaxResult.returnSuccess().put( "value", rtnResourceMap );
    }

    /**
     * 不合格代码列表
     *
     * @param ncGroup
     * @return
     */
    @RequestMapping("/ncGroup/ncCode")
    public AjaxResult findNCCodeList( @RequestParam String ncGroup ){
        String site = CommonMethods.getSite();
        Assert.hasText( ncGroup, I18nUtil.getI18nText( "common.ncGroupNotBlank" ) );

        List<Map<String,Object>> ncCodeList = ncGroupAdviceService.findNCGroupMember( site, ncGroup );
        List<Map<String,Object>> rtnNcCodeList = new ArrayList<>();
        for( Map<String,Object> ncCodeMap : ncCodeList ){
            Map<String,Object> rtnNoCodeMap = new HashMap<>();
            rtnNoCodeMap.put( "ncCode", ncCodeMap.get( "NC_CODE" ) );
            rtnNoCodeMap.put( "description", ncCodeMap.get( "DESCRIPTION" ) );
            rtnNcCodeList.add( rtnNoCodeMap );
        }
        return AjaxResult.returnSuccess().put( "value", rtnNcCodeList );
    }

    /**
     * 设备不合格代码
     *
     * @param resource
     * @return
     */
    @RequestMapping("/resource/ncCode")
    public AjaxResult findNCCodeByResource( @RequestParam String resource ){
        String site = CommonMethods.getSite();
        Assert.hasText( resource, I18nUtil.getI18nText( "common.resourceNotBlank" ) );

        String operation = resourceAdviceService.findOperationByResource( site, resource );
        List<String> ncGroupList = ncGroupAdviceService.findNCGroupValidOper( site, operation );

        List<Map<String,Object>> rtnNcCodeList = new ArrayList<>();
        for( String ncGroupBo : ncGroupList ){
            List<Map<String,Object>> ncCodeList = ncGroupAdviceService.findNCGroupMember( ncGroupBo );
            for( Map<String,Object> ncCodeMap : ncCodeList ){
                Map<String,Object> rtnNoCodeMap = new HashMap<>();
                rtnNoCodeMap.put( "ncCode", ncCodeMap.get( "NC_CODE" ) );
                rtnNoCodeMap.put( "description", ncCodeMap.get( "DESCRIPTION" ) );
                rtnNcCodeList.add( rtnNoCodeMap );
            }
        }
        return AjaxResult.returnSuccess().put( "value", rtnNcCodeList );
    }

    /**
     * 验证不合格代码
     *
     * @param ncCode
     * @param resource
     * @return
     */
    @RequestMapping("/resource/existNCCode")
    public AjaxResult existNCCodeByResource( @RequestParam String ncCode, @RequestParam String resource ){
        String site = CommonMethods.getSite();
        Assert.hasText( ncCode, I18nUtil.getI18nText( "common.ncCodeNotBlank" ) );
        Assert.hasText( resource, I18nUtil.getI18nText( "common.resourceNotBlank" ) );

        Map<String,Object> ncCodeMap = ncCodeAdviceService.existNCCode( site, ncCode );

        String operation = resourceAdviceService.findOperationByResource( site, resource );
        List<String> ncGroupList = ncGroupAdviceService.findNCGroupValidOper( site, operation );

        Map<String,Object> rtnNcCodeMap = new HashMap<>();
        for( String ncGroupBo : ncGroupList ){
            List<Map<String,Object>> ncCodeList = ncGroupAdviceService.findNCGroupMember( ncGroupBo );
            for( Map<String,Object> _ncCodeMap : ncCodeList ){
                if( ncCode.equals( _ncCodeMap.get( "NC_CODE" ) ) ){
                    rtnNcCodeMap.put( "ncCode", ncCodeMap.get( "NC_CODE" ) );
                    rtnNcCodeMap.put( "description", ncCodeMap.get( "DESCRIPTION" ) );
                    break;
                }
            }
        }
        if( rtnNcCodeMap.isEmpty() ){
            throw BusinessException.build("common.ncCodeNotMatchOperation", new String[]{ ncCode, operation }  );
        }else {
            return AjaxResult.returnSuccess().put( "value", rtnNcCodeMap );
        }
    }

    /**
     * 不合格代码
     *
     * @param ncCode
     * @return
     */
    @RequestMapping("/ncCode")
    public AjaxResult findNcCode( @RequestParam String ncCode ){
        String site = CommonMethods.getSite();
        String ncGroup = ServletUtils.getParameter( "ncGroup" );
        Assert.hasText( ncCode, I18nUtil.getI18nText( "common.ncCodeNotBlank" ) );

        Map<String,Object> ncCodeMap = ncCodeAdviceService.existNCCode( site, ncCode );
        if( StringUtils.isNotBlank( ncGroup ) ){
            List<Map<String,Object>> ncCodeList = ncGroupAdviceService.findNCGroupMember( site, ncGroup );
            if( !ncCodeList.contains( ncCodeMap ) ){
                throw BusinessException.build( "common.ncCodeNotMatchNcGroup", new String[]{ ncCode, ncGroup } );
            }
        }

        Map<String,Object> rtnNcCodeMap = new HashMap<>();
        rtnNcCodeMap.put( "ncCode", ncCodeMap.get( "NC_CODE" ) );
        rtnNcCodeMap.put( "description", ncCodeMap.get( "DESCRIPTION" ) );
        return AjaxResult.returnSuccess().put( "value", rtnNcCodeMap );
    }

    /**
     * 物料包装容器
     *
     * @param itemBo
     * @param container
     * @return
     */
    @RequestMapping("/item/container")
    public AjaxResult findContainer( @RequestParam String itemBo, @RequestParam String container ){
        String site = CommonMethods.getSite();
        Assert.hasText( itemBo, I18nUtil.getI18nText( "common.itemNotBlank" ) );
        Assert.hasText( container, I18nUtil.getI18nText( "common.containerNotBlank" ) );
        Map<String,Object> packLevelMap = containerAdviceService.findItemPackingLevel( site, container, itemBo );
        Map<String,Object> containerMap = new HashMap<>();
        if( packLevelMap != null ){
            containerMap.put( "container", BOHandleUtil.splitContainerHandle( (String) packLevelMap.get( "CONTAINER_BO" ) )[1] );
            containerMap.put( "maxQty", packLevelMap.get( "MAX_QTY" ) );
        }
        return AjaxResult.returnSuccess().put( "value", containerMap );
    }

    /**
     * 存储地点
     *
     * @return
     */
    @RequestMapping("/list/storageLocation")
    public AjaxResult findStorageLocationList(){
        String site = CommonMethods.getSite();

        List<Map<String,Object>> storageLocationList = storageLocationAdviceMapper.findStorageLcationList( site );
        List<Map<String,Object>> rtnStorageLocationList = new ArrayList<>();
        for( Map<String,Object> storageLocationMap : storageLocationList ){
            Map<String,Object> rtnStorageLocationMap = new HashMap<>();
            rtnStorageLocationMap.put( "storageLocation", storageLocationMap.get( "STORAGE_LOCATION" ) );
            rtnStorageLocationMap.put( "description", storageLocationMap.get( "DESCRIPTION" ) );
            rtnStorageLocationList.add( rtnStorageLocationMap );
        }

        return AjaxResult.returnSuccess().put( "value", rtnStorageLocationList );
    }

    /**
     * 存储地点
     *
     * @param storageLocation
     * @return
     */
    @RequestMapping("/storageLocation")
    public AjaxResult findStorageLocation( @RequestParam String storageLocation ){
        String site = CommonMethods.getSite();
        Assert.hasText( storageLocation, I18nUtil.getI18nText( "common.storageLocationNotBlank" ) );

        Map<String,Object> storageLocationMap = storageLocationAdviceMapper.findStorageLcation( site, storageLocation );
        Assert.notNull( storageLocationMap, I18nUtil.getI18nText( "common.storageLocationNotExist", new String[]{ storageLocation } ) );

        List<Map<String,Object>> rtnStorageLocationList = new ArrayList<>();
        Map<String,Object> rtnStorageLocationMap = new HashMap<>();
        rtnStorageLocationMap.put( "storageLocation", storageLocationMap.get( "STORAGE_LOCATION" ) );
        rtnStorageLocationMap.put( "description", storageLocationMap.get( "DESCRIPTION" ) );
        rtnStorageLocationList.add( rtnStorageLocationMap );

        return AjaxResult.returnSuccess().put( "value", rtnStorageLocationMap );
    }

    /**
     * 领料单列表
     *
     * @return
     */
    @RequestMapping("/picking")
    public AjaxResult findPickingList(  ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        List< PickingBillLog > pickingBillLogs = pickingMapper.selectPicking( site );
        return AjaxResult.returnSuccess().put( "value", pickingBillLogs );
    }

    /**
     * 物料清单物料列表
     *
     * @return
     */
    @RequestMapping("/BomitemBysite")
    public AjaxResult BomitemBysite(  ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        String machineType = itemGroupAdviceService.findItemGroupBySite( site );
        List< Map<String,Object> >  bomcomponentlList = itemAdviceService.findBomcomponentlList( site , machineType );
        if ( bomcomponentlList == null ){
            throw BusinessException.build("production.picking.SiteBomnotExist",new String[]{site,machineType});
        }
        List< Map<String,Object> > returnMap = new ArrayList<>();
        for( Map<String,Object> itemMemberMap : bomcomponentlList ){
            Map<String,Object> rtnItemMap = new HashMap<>();
            Map<String,Object> item = itemAdviceService.existItem( (String) itemMemberMap.get("COMPONENT_GBO") )  ;
            rtnItemMap.put("item",item.get("ITEM"));
            rtnItemMap.put("description" , item.get("DESCRIPTION"));
            rtnItemMap.put("unit",item.get("UNIT_OF_MEASURE"));

            returnMap.add( rtnItemMap );
        }


        return AjaxResult.returnSuccess().put( "value", returnMap );
    }



    /**
     * 验证领料单
     *
     * @return
     */
    @RequestMapping("/picking/bill")
    public AjaxResult exsitPickingBill( @RequestParam String bill ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        PickingBillLog pickingBillLog = pickingBillLogMapper.selectPickingBillLogById( BOHandleUtil.buildPickingBillLogHandle( site,bill ) );

        if( pickingBillLog == null ){
            throw BusinessException.build("production.picking.BillNo", new String[]{ bill }  );
        }else {
            return AjaxResult.returnSuccess().put( "value", pickingBillLog );
        }
    }


    /**
     * sfc带出物料清单物料列表(CNC/GRIND),并计算数量,
     *
     * @param  sfc
     * @return
     */
    @RequestMapping("/ItemListBySfc")
    public AjaxResult findRawMaterialList(@RequestParam String sfc  ){

            String site = CommonMethods.getSite();
            Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
            SFC sfc1 = sfcMapper.selectSFCById( BOHandleUtil.buildSFCHandle( site,sfc ) );
            Assert.notNull( sfc1, I18nUtil.getI18nText( "production.sfc.sfcNotExist", new String[]{ sfc } ) );
           //String status = "StatusBO:"+ site +",401";
           /**检查工令状态*/
           /*if ( !sfc1.getStatusBo().equals( status )  ){
            throw BusinessException.build("production.picking.sfcStatus",new String[]{ sfc1.getSfc(),sfc1.getStatusBo() });
            }*/
            Long qty = sfc1.getSfcQty();
            String produce = sfc1.getProcedureBo().split(",")[1];
            if ( qty == 0L ){
                throw BusinessException.build( "production.sfc.sfcQtyGreaterThanZero" );
            }
            BigDecimal a = new BigDecimal(qty);

            String machineType = itemGroupAdviceService.findItemGroupBySite( site );
            List< Map<String,Object> >  bomcomponentlList = itemAdviceService.findBomcomponentlList( site , machineType );
            Map<String,Object> returnMap = new HashMap<>();
            returnMap.put("sfcQty",qty);
            List< Map<String,Object> > itemlist = new ArrayList<>();
            for ( Map<String,Object> map : bomcomponentlList ){

                BigDecimal number = (BigDecimal)map.get("QTY");
                number=number.multiply(a);

                String procedure = customFieldsAdviceService.findCustomValue( (String) map.get("HANDLE") ,Constants.BOM_COMPONENT_CUSTOM_FIELD_PROCEDURE );
                if( procedure.equals( produce ) ){
                    String  Handle = (String ) map.get( "COMPONENT_GBO" );
                    Map<String,Object> itemMap = itemAdviceMapper.selectItemMap( Handle );
                    itemMap.put( "QTY" , number );

                    itemlist.add( itemMap );
                }

            }
            List<Map<String,Object>> rawmateriallist = new ArrayList<>();

            if ( itemlist.isEmpty() ){
                returnMap.put("bomlist",rawmateriallist);

            }else {
                for ( Map<String,Object> RawmaterialMap : itemlist){

                    Map<String,Object> rtnRawmaterialMap = new HashMap<>();
                    rtnRawmaterialMap.put( "item" , RawmaterialMap.get("ITEM"));
                    rtnRawmaterialMap.put( "description" , RawmaterialMap.get("DESCRIPTION"));
                    rtnRawmaterialMap.put( "quantityRestriction" , RawmaterialMap.get("QUANTITY_RESTRICTION"));
                    rtnRawmaterialMap.put( "unit" , RawmaterialMap.get("UNIT_OF_MEASURE"));
                    rtnRawmaterialMap.put( "qty" , RawmaterialMap.get("QTY"));
                    rtnRawmaterialMap.put("remark",sfc);
                    rawmateriallist.add( rtnRawmaterialMap );
                    returnMap.put("bomlist",rawmateriallist);
                }
            }

            return  AjaxResult.returnSuccess().put( "value" ,returnMap );
    }
    /**
     * 验证物料
     *
     * @return
     */
    @RequestMapping("/itemBysite")
    public AjaxResult finditemBysite(@RequestParam String item ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        Map<String,Object> itemMap = itemAdviceService.existItem( site,item );
        return AjaxResult.returnSuccess().put( "value", itemMap );
    }

    /**
     * 站点原材料列表
     *
     * @return
     */
    @RequestMapping("/findMaterial")
    public AjaxResult findMaterial(){
        String site = CommonMethods.getSite();
        List<Map<String,Object>> materiallist= itemAdviceMapper.findRawMaterialList( site );
        List<Map<String,Object>> retMaterial = new ArrayList<>();
        for ( Map<String,Object> map : materiallist ){
            Map<String,Object> map1 = new HashMap<>();
            map1.put( "item" , map.get("ITEM"));
            map1.put( "description" , map.get("DESCRIPTION"));
            map1.put( "quantityRestriction" , map.get("QUANTITY_RESTRICTION"));
            map1.put( "unit" , map.get("UNIT_OF_MEASURE"));
            retMaterial.add( map1 );
        }
        return AjaxResult.returnSuccess().put("value",retMaterial);
    }

    /**
     * 站点物料列表
     *
     * @return
     */
    @RequestMapping("/findItemSite")
    public AjaxResult findItemSite(){
        String site = CommonMethods.getSite();
        List<Map<String,Object>> itemllist= itemAdviceMapper.findItemSite( site );
        List<Map<String,Object>> retItem = new ArrayList<>();
        for ( Map<String,Object> map : itemllist ){
            Map<String,Object> map1 = new HashMap<>();
            map1.put( "item" , map.get("ITEM"));
            map1.put( "description" , map.get("DESCRIPTION"));
            map1.put( "quantityRestriction" , map.get("QUANTITY_RESTRICTION"));
            map1.put( "unit" , map.get("UNIT_OF_MEASURE"));
            retItem.add( map1 );
        }
        return AjaxResult.returnSuccess().put("value",retItem);
    }

    /**
     * 站点所有机种
     *
     * @return
     */
    @RequestMapping("/machineType")
    public AjaxResult findMachineType(  ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        List<Map<String,Object>> list = itemGroupAdviceService.selectItemGroupMapList( site );
        List<Map<String,Object>> returnlist = new ArrayList<>();
        for (Map<String,Object> map : list ){
           Map<String,Object>  trainMap = new HashMap<>();
           trainMap.put( "machineType" , map.get("ITEM_GROUP") );
           trainMap.put( "description",map.get("DESCRIPTION") );

           returnlist.add( trainMap );
        }

        return AjaxResult.returnSuccess().put( "value", returnlist );
    }

    /**
     * 站点所有不良代码
     *
     * @return
     */
    @RequestMapping("/ncCodeBySite")
    public AjaxResult findNcCodeBysite(  ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        List<Map<String, Object>> nclist = ncCodeAdviceService.findNcCodeBySite( site );
        List<Map<String, Object>>  ncCode = new ArrayList<>();
        for ( Map<String, Object> map : nclist ){
            Map<String, Object> map_ = new HashMap<>();
            map_.put("ncCode" , map.get("NC_CODE"));
            map_.put("description" , map.get("DESCRIPTION"));
            ncCode.add( map_ );
        }
        return AjaxResult.returnSuccess().put( "value", ncCode );
    }

    /**
     * 站点所有资源
     *
     * @return
     */
    @RequestMapping("/resrceBySite")
    public AjaxResult findResrceBysite(  ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));

        List<Map<String,Object>> list = resourceAdviceService.findResrceBySite( site );
        List<Map<String,Object>> resrcelist = new ArrayList<>();
        for (Map<String,Object> map : list){
            Map<String,Object> resrceMap = new HashMap<>();

            resrceMap.put( "resrce" , map.get("RESRCE") );
            resrceMap.put("description" , map.get("DESCRIPTION"));
            resrcelist.add(resrceMap);
        }

        return AjaxResult.returnSuccess().put( "value", resrcelist );
    }

    /**
     * 部门列表
     *
     * @return
     */
    @RequestMapping("/DeptBySite")
    public AjaxResult findDeptBysite(  ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        List< FieldGroup > fieldGroupList = fieldGroupAdviceService.findFieldListByGroup( site , Constants.FIELD_GROUP_DEPT );

        return AjaxResult.returnSuccess().put( "value", fieldGroupList );
    }

    /**
     * 验证部门
     *
     * @return
     */
    @RequestMapping("/exsitDept")
    public AjaxResult exsitDept( @RequestParam String dept ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        FieldGroup fieldGroup = fieldGroupAdviceService.existFieldGroup( site , Constants.FIELD_GROUP_DEPT , dept );

        return AjaxResult.returnSuccess().put( "value", fieldGroup );
    }

    /**
     * 换刀类型列表
     *
     * @return
     */
    @RequestMapping("/BreakType")
    public AjaxResult findBreakType(  ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        List< FieldGroup > fieldGroupList = fieldGroupAdviceService.findFieldListByGroup( site , Constants.FIELD_GROUP_BreakType);

        return AjaxResult.returnSuccess().put( "value", fieldGroupList );
    }

    /**
     * 刀具类型列表
     *
     * @return
     */
    @RequestMapping("/ToolType")
    public AjaxResult findToolType(  ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        List<TOOL> tools = toolBreakAdiviceMapper.selectToolBysite( site );

        return AjaxResult.returnSuccess().put( "value", tools  );
    }
    /**
     * 验证刀具类型
     *
     * @return
     */
    @RequestMapping("/existToolType")
    public AjaxResult existToolType( @RequestParam String toolType ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        Assert.hasText( toolType, I18nUtil.getI18nText( "production.tool.toolType" ));

        TOOL tool =toolAdiviceService.selectToolMap( toolType ,site );
        Map<String,Object> retmap = new HashMap<>();
        List<TOOL> toollist = new ArrayList<>();
        toollist.add( tool );
        retmap.put("toollist", toollist );
        retmap.put("toolType",toolType);
        return AjaxResult.returnSuccess().put("value",retmap);
    }

    /**
     * 刀具清单列表
     *
     * @return
     */
    @RequestMapping("/ToolList")
    public AjaxResult findToolList( @RequestParam String machineType, @RequestParam String cncType ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));

        ToolBill queryParam = new ToolBill();
        queryParam.setSite( site );
        queryParam.setMachineType( machineType );
        queryParam.setCncType( cncType );

        List< ToolBill > tools =  ToolBillService.selectToolBillList( queryParam );

        return AjaxResult.returnSuccess().put( "value", tools  );
    }

    /**
     * 客户列表
     *
     * @return
     */
    @RequestMapping("/CustomerList")
    public AjaxResult findCustomerList( ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));

        List<Map<String,Object>> customerlist = customerMapper.selectCustomerlist( site );
        List<Map<String,Object>> retCustomer = new ArrayList<>();
        for (Map<String,Object> map : customerlist ){
            Map<String,Object> retMap = new HashMap<>();
            retMap.put( "customer" , map.get("CUSTOMER") );
            retMap.put( "description",map.get("CUSTOMER_NAME") );

            retCustomer.add( retMap );
        }

        return AjaxResult.returnSuccess().put( "value", retCustomer  );
    }

    /**
     * 验证客户
     *
     * @return
     */
    @RequestMapping("/exsitCustomer")
    public AjaxResult exsitCustomer( @RequestParam String customer ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        Assert.hasText( customer, I18nUtil.getI18nText( "common.customerNotBlank" ));

        Map<String,Object> map =  customerService.existCustomer( site , customer );

        Map<String,Object> customerMap = new HashMap<>();

        customerMap.put("customer" , map.get("CUSTOMER"));
        customerMap.put( "description",map.get("CUSTOMER_NAME") );


        return AjaxResult.returnSuccess().put( "value", customerMap  );
    }

    /**
     * 客诉类型列表
     *
     * @return
     */
    @RequestMapping("/CmplnTypeList")
    public AjaxResult findCmplnTypeList( ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));

        List< FieldGroup > fieldGroupList = fieldGroupAdviceService.findFieldListByGroup( site , Constants.FIELD_GROUP_CmplnType );

        return AjaxResult.returnSuccess().put( "value", fieldGroupList  );
    }

    /**
     * 领料单机种列表
     *
     * @return
     */
    @RequestMapping("/picking/device")
    public AjaxResult finddeviceList( ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));

        List< FieldGroup > fieldGroupList = fieldGroupAdviceService.findFieldListByGroup( site , Constants.FIELD_GROUP_PICKING_MACHINE_TYPE );

        return AjaxResult.returnSuccess().put( "value", fieldGroupList  );
    }

    /**
     * 验证领料单机种
     *
     * @return
     */
    @RequestMapping("/picking/exsitdevice")
    public AjaxResult exsitdevice( @RequestParam String machineType ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        FieldGroup fieldGroup = fieldGroupAdviceService.existFieldGroup( site , Constants.FIELD_GROUP_PICKING_MACHINE_TYPE, machineType );

        return AjaxResult.returnSuccess().put( "value", fieldGroup );
    }

    @RequestMapping("/machineType/alternativeItem")
    public AjaxResult findAlternativeItemList(){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        String itemGroup = itemGroupAdviceService.findItemGroupBySite( site );
        List<Map<String,Object>> itemTreeList = new ArrayList<>();
        commonService.findItemTree( site, itemGroup, itemTreeList, true, "M" );

        List<Map<String,Object>> rtnItemList = new ArrayList<>();
        for( Map<String,Object> itemMap : itemTreeList ){
            Map<String,Object> rtnItemMap = new HashMap<>();
            rtnItemMap.put( "machineType", itemMap.get( "ITEM_GROUP" ) );
            rtnItemMap.put( "itemBo", itemMap.get( "HANDLE" ) );
            rtnItemMap.put( "item", itemMap.get( "ITEM" ) );
            rtnItemMap.put( "revision", itemMap.get( "REVISION" ) );
            rtnItemMap.put( "description", customFieldsAdviceService.existCustomValue((String) itemMap.get( "HANDLE" ), Constants.ITEM_CUSTOM_FIELD_ITEM_LABEL ) );
            rtnItemList.add( rtnItemMap );
        }
        return AjaxResult.returnSuccess().put( "value", rtnItemList );
    }

    /**
     * 工具组列表
     * @return
     */
    @RequestMapping("/toolGroup/findtoolGroup")
    public AjaxResult findtoolGroup( ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        String status = "StatusBO:" + site + ",101";
        List<Map<String,Object>> tool =  findUndoneMapper.findToolGroupBySite( site , status );
        List<Map<String,Object>> resList = new ArrayList<>();
        for (Map<String,Object> map : tool){
            Map<String,Object> resMap = new HashMap<>();
            resMap.put("group",map.get("TOOL_GROUP"));
            resMap.put("description",map.get("DESCRIPTION"));
            resList.add( resMap );
        }

        return AjaxResult.returnSuccess().put( "value", resList );
    }

    /**
     * 验证工具组
     * @return
     */
    @RequestMapping("/toolGroup/exsittoolGroup")
    public AjaxResult exsittoolGroup( String group ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        String handle = BOHandleUtil.buildToolGroupHandle( site , group );
        String status = "StatusBO:" + site + ",101";
        Map<String,Object> map =  findUndoneMapper.findToolGroupByHandle( handle );
        Assert.notNull( map , I18nUtil.getI18nText( "common.toolGroupNotExist", new String[]{ handle } ) );
        if ( !status.equals(  map.get("STATUS_BO") ) ){
           throw BusinessException.build("common.toolGroupStatusIsError",new String[]{ handle });
        }

        Map<String,Object> resMap = new HashMap<>();
        resMap.put("group",map.get("TOOL_GROUP"));
        resMap.put("description",map.get("DESCRIPTION"));

        return AjaxResult.returnSuccess().put( "value", resMap );
    }

    /**
     * 领料工序列表
     *
     * @return
     */
    @RequestMapping("/pick/operation")
    public AjaxResult operationBySite(  ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        List< FieldGroup > fieldGroupList = fieldGroupAdviceService.findFieldListByGroup( site , Constants.ITEM_GROUP_CUSTOM_FIELD_PICK_OPERATION );

        return AjaxResult.returnSuccess().put( "value", fieldGroupList );
    }

    /**
     * 验证领料工序
     *
     * @return
     */
    @RequestMapping("/pick/exsitOperation")
    public AjaxResult exsitOperation( @RequestParam String operation ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        FieldGroup fieldGroup = fieldGroupAdviceService.existFieldGroup( site , Constants.ITEM_GROUP_CUSTOM_FIELD_PICK_OPERATION , operation );

        return AjaxResult.returnSuccess().put( "value", fieldGroup );
    }

    /**
     * cpk测量项弹出框
     *
     * @return
     */
    @RequestMapping("/cpk/findFail")
    public AjaxResult findFail( String cncType ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        String machineType = itemGroupAdviceService.findItemGroupBySite( site );
        /**检查夹位*/
        fieldGroupAdviceService.existFieldGroup( site , Constants.FIELD_GROUP_CncType , cncType );
        cncType = cncType.substring(2,6);
        String dcBo =machineType + "_"+cncType;

        List<Map<String,Object >> parameterlist = dcGroupAdviceService.findDCParameterList( site , dcBo );
        if ( parameterlist.isEmpty() ){
            throw BusinessException.build("qc.point.dcGroupnotBlank",new String[]{dcBo});
        }
        List<Map<String,Object >> rtnlist = new ArrayList<>();

        for ( Map<String,Object> map : parameterlist ){
            Map<String,Object> rtnMap = new HashMap<>() ;
            rtnMap.put( "parameterName",map.get("PARAMETER_NAME") );
            rtnMap.put( "description",map.get("DESCRIPTION") );

            rtnlist.add( rtnMap );
        }

        return AjaxResult.returnSuccess().put( "value", rtnlist );
    }
}
